#循环语句
for (i in 1:10) print("Hello")

i <- 10
while (i > 0){
  print("Hello");
  i = i - 1
}

x <- 2.0
if (x < 0.2){
  x <- x + 1;
  print("sucess")
}else {
  x <- x - 1;
  print("else sucess")
}

#不一样的地方

#ifelse循环 有点像三目表达式
x <- c(1,1,1,0,0,1,1)
ifelse(x != 1, 1, 0) #向量里不是1的就是1

#switch 这个switch省略了case
result <- 4
switch(result, "低异常点", "偏低", "正常", "偏高", "高异常点")

#函数
mystats<- function(x, parametric=FALSE, print=TRUE) {
  if (parametric) {
    center<- mean(x);
    spread<- sd(x)
  } else {
    center <- median(x);
    spread<- mad(x)
  }
  if (print & parametric) {
    cat("mean=", center, "\n", "mad=", spread, "\n")
  } else if (print & !parametric) {
    cat("median=", center, "\n", "mad=", spread, "\n")
  }
  result <- list(center=center, spread=spread)
  return(result)
}
set.seed(1234)
x <- rnorm(500)
y <- mystats(x)
y <- mystats(x, parametric=FALSE, print=TRUE)

myfunction <- function(x) {
  if (x %% 2 == 0){
    print("偶数") 
  }else {
    print("奇数")
  }
}
myfunction(23)

